
package com.example.haoge.study_expressage;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.SurfaceView;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.haoge.study_expressage.zxing.ScanListener;
import com.example.haoge.study_expressage.zxing.ScanManager;
import com.example.haoge.study_expressage.zxing.decode.DecodeThread;
import com.example.haoge.study_expressage.zxing.decode.Utils;
import com.google.zxing.Result;


public final class CommonScanActivity extends Activity implements ScanListener,
		View.OnClickListener {
	static final String TAG = CommonScanActivity.class.getSimpleName();
	SurfaceView scanPreview = null;
	View scanContainer;
	View scanCropView;
	ImageView scanLine;
	ScanManager scanManager;
	TextView iv_light;
	TextView qrcode_g_gallery;
	TextView qrcode_ic_back;
	final int PHOTOREQUESTCODE = 1111;

	Button rescan;
	ImageView scan_image;
	ImageView authorize_return;
	private int scanMode;// 扫描模型（条形，二维码，全部）

	TextView title;
	TextView scan_hint;
	TextView tv_scan_result;

	@Override
	public void onCreate(Bundle icicle) {
		super.onCreate(icicle);

		Window window = getWindow();
		window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
		setContentView(R.layout.activity_scan_code);
		scanMode = getIntent().getIntExtra(Constant.REQUEST_SCAN_MODE,
				Constant.REQUEST_SCAN_MODE_ALL_MODE);
		init();

		initView();
	}


	private void init() {
		rescan = (Button) findViewById(R.id.service_register_rescan);
		scan_image = (ImageView) findViewById(R.id.scan_image);
		authorize_return = (ImageView) findViewById(R.id.authorize_return);
		title = (TextView) findViewById(R.id.common_title_TV_center);
		scan_hint = (TextView) findViewById(R.id.scan_hint);
		tv_scan_result = (TextView) findViewById(R.id.tv_scan_result);
	}

	void initView() {
		switch (scanMode) {

		case DecodeThread.ALL_MODE:
			title.setText(R.string.scan_allcode_title);
			scan_hint.setText(R.string.scan_allcode_hint);
			break;
		}
		scanPreview = (SurfaceView) findViewById(R.id.capture_preview);
		scanContainer = findViewById(R.id.capture_container);
		scanCropView = findViewById(R.id.capture_crop_view);
		scanLine = (ImageView) findViewById(R.id.capture_scan_line);
		qrcode_g_gallery = (TextView) findViewById(R.id.qrcode_g_gallery);
		qrcode_g_gallery.setOnClickListener(this);
		qrcode_ic_back = (TextView) findViewById(R.id.qrcode_ic_back);
		qrcode_ic_back.setOnClickListener(this);
		iv_light = (TextView) findViewById(R.id.iv_light);
		iv_light.setOnClickListener(this);
		rescan.setOnClickListener(this);
		authorize_return.setOnClickListener(this);
		// 构造出扫描管理器
		scanManager = new ScanManager(this, scanPreview, scanContainer,
				scanCropView, scanLine, scanMode, this);
	}

	@Override
	public void onResume() {
		super.onResume();
		scanManager.onResume();
		rescan.setVisibility(View.INVISIBLE);
		scan_image.setVisibility(View.GONE);
	}

	@Override
	public void onPause() {
		super.onPause();
		scanManager.onPause();
	}

	/**
     *
     */
	public void scanResult(Result rawResult, Bundle bundle) {
		// 扫描成功后，扫描器不会再连续扫描，如需连续扫描，调用reScan()方法。
		// scanManager.reScan();
		// Toast.makeText(that, "result="+rawResult.getText(),
		// Toast.LENGTH_LONG).show();

		if (!scanManager.isScanning()) { // 如果当前不是在扫描状态
			// 设置再次扫描按钮出现
			rescan.setVisibility(View.VISIBLE);
			scan_image.setVisibility(View.VISIBLE);
			Bitmap barcode = null;
			byte[] compressedBitmap = bundle
					.getByteArray(DecodeThread.BARCODE_BITMAP);
			if (compressedBitmap != null) {
				barcode = BitmapFactory.decodeByteArray(compressedBitmap, 0,
						compressedBitmap.length, null);
				barcode = barcode.copy(Bitmap.Config.ARGB_8888, true);
			}
			scan_image.setImageBitmap(barcode);
		}
		rescan.setVisibility(View.VISIBLE);
		scan_image.setVisibility(View.VISIBLE);

		Intent  it=new Intent(CommonScanActivity.this,MainActivity.class);
		it.putExtra("number",rawResult.getText());
		startActivity(it);
	}

	void startScan() {
		if (rescan.getVisibility() == View.VISIBLE) {
			rescan.setVisibility(View.INVISIBLE);
			scan_image.setVisibility(View.GONE);
			scanManager.reScan();
		}
	}

	@Override
	public void scanError(Exception e) {
		Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
		// 相机扫描出错时
		if (e.getMessage() != null && e.getMessage().startsWith("相机")) {
			scanPreview.setVisibility(View.INVISIBLE);
		}
	}

	public void showPictures(int requestCode) {
		Intent intent = new Intent(Intent.ACTION_PICK);
		intent.setType("image/*");
		startActivityForResult(intent, requestCode);
	}

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		String photo_path;
		if (resultCode == RESULT_OK) {
			switch (requestCode) {
			case PHOTOREQUESTCODE:
				String[] proj = { MediaStore.Images.Media.DATA };
				Cursor cursor = this.getContentResolver().query(data.getData(),
						proj, null, null, null);
				if (cursor.moveToFirst()) {
					int colum_index = cursor
							.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
					photo_path = cursor.getString(colum_index);
					if (photo_path == null) {
						photo_path = Utils.getPath(getApplicationContext(),
								data.getData());
					}
					scanManager.scanningImage(photo_path);
				}
			}
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.qrcode_g_gallery:
			showPictures(PHOTOREQUESTCODE);
			break;
		case R.id.iv_light:
			scanManager.switchLight();
			break;
		case R.id.qrcode_ic_back:
			finish();
			break;
		case R.id.service_register_rescan:// 再次开启扫描
			startScan();
			break;
		case R.id.authorize_return:
			finish();
			break;
		default:
			break;
		}
	}

}